//
//  TWBNavigationViewController.swift
//  weiBo
//
//  Created by 杨雷 on 2016/12/12.
//  Copyright © 2016年 Yang.L. All rights reserved.
//

import UIKit

class TWBNavigationViewController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.interactivePopGestureRecognizer?.delegate = self
        
    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if viewControllers.count > 0 {
            
            viewController.hidesBottomBarWhenPushed = true
        }
        
        super.pushViewController(viewController, animated: animated)
        
        if viewControllers.count > 1 {
            
            if viewControllers.count == 2 {
                
                let title = viewControllers.first!.title!
                
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title, imageName: "navigationbar_back_withtext", target: self, action: #selector(popAction))
                
            } else {
               
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", imageName: "navigationbar_back_withtext", target: self, action: #selector(popAction))
                
            }
            
            viewController.title = "当前显示的是第\(viewControllers.count)级控制器"
            
        }
        
        
        
    }
    
    @objc private func popAction() {
        popViewController(animated: true)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension TWBNavigationViewController: UIGestureRecognizerDelegate {
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        if viewControllers.count == 1 {
            
            return false
        }
        
        return true
    }
}

